package com.nanyang.tms.controller.storedrule;

import java.util.List;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.tms.api.dto.request.storedrule.StoredRuleAddRequest;
import com.nanyang.tms.api.dto.request.storedrule.StoredRuleUpdateRequest;
import com.nanyang.tms.api.dto.request.storedrule.StoredRuleUpdateStateRequest;
import com.nanyang.tms.api.dto.response.storedrule.StoredRuleResponse;
import com.nanyang.tms.api.service.storerule.StoredRuleRpcService;

@RestController
@RequestMapping("/storedrule")
public class StoredRuleController {
    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private StoredRuleRpcService storedRuleRpcService;

    /**
     * <p>listStoredRule</p>
     * <p>Description:查询服务商列表</p>
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<List<StoredRuleResponse>> listStoredRule() {
        ApiResult<List<StoredRuleResponse>> apiResult = storedRuleRpcService.listStoredRule();
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listStoredRule</p>
     * <p>Description:查询根据编码</p>
     *
     * @param ruleCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{ruleCode}", method = RequestMethod.GET)
    public ApiResult<StoredRuleResponse> selectByCode(@PathVariable("ruleCode") String ruleCode) {
        ApiResult<StoredRuleResponse> apiResult = storedRuleRpcService.selectByCode(ruleCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @BindParam
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiResult<Boolean> saveStoredRule( @BindParam @RequestBody @Valid StoredRuleAddRequest param) {
        ApiResult<Boolean> apiResult = storedRuleRpcService.saveStoredRule(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResult<Boolean> updateStoredRule(@BindParam @RequestBody @Valid StoredRuleUpdateRequest param) {
        ApiResult<Boolean> apiResult = storedRuleRpcService.updateStoredRule(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 禁用
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public ApiResult<Boolean> disableState(@BindParam @RequestBody @Valid StoredRuleUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = storedRuleRpcService.disableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 启用
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public ApiResult<Boolean> enableState(@BindParam @RequestBody @Valid StoredRuleUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = storedRuleRpcService.enableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
